package com.qiyi.player.wonder.body.model.actors.parts
{
   import flash.events.EventDispatcher;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.wonder.common.config.SystemConfig;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.adobe.serialization.json.JSON;
   import com.qiyi.player.base.logging.Log;
   
   public class CollectStateInfo extends EventDispatcher
   {
      
      public function CollectStateInfo() {
         this._log = Log.getLogger("com.qiyi.player.wonder.body.model.actors.parts.CollectStateControl");
         super();
      }
      
      public static const EVENT_COLLECT_STATE_CHANGE:String = "eventCollectStateChange";
      
      private var _loader:URLLoader;
      
      private var _request:URLRequest;
      
      private var _tvID:String = "";
      
      private var _isCollect:Boolean = false;
      
      private var _isHasCollect:Boolean = false;
      
      private var _log:ILogger;
      
      public function requestCollect(param1:String, param2:Boolean) : void {
         var _loc3_:String = null;
         var _loc4_:String = null;
         if(param1)
         {
            this.destroyLoader();
            this._tvID = param1;
            _loc3_ = param2?SystemConfig.LOGIN_REQUEST_COLLECT:SystemConfig.UNLOGIN_REQUEST_COLLECT;
            _loc4_ = _loc3_ + "?tvId=" + param1 + "&terminalId=11" + "&tn=" + Math.floor(Math.random() * 100000);
            this._request = new URLRequest(_loc4_);
            this._loader = new URLLoader();
            this._loader.addEventListener(Event.COMPLETE,this.onCollectCompleteHandler);
            this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
            this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
            this._loader.load(this._request);
            this._log.info("CollectStateInfo requestCollect : " + _loc4_);
         }
      }
      
      private function onCollectCompleteHandler(param1:Event) : void {
         var jsonInfo:Object = null;
         var var_2:Event = param1;
         try
         {
            jsonInfo = com.adobe.serialization.json.JSON.decode(this._loader.data);
            if(!(jsonInfo.code == undefined) && jsonInfo.code == "A00000")
            {
               this._isCollect = true;
               this._isHasCollect = true;
               dispatchEvent(new Event(EVENT_COLLECT_STATE_CHANGE));
               this._log.info("CollectStateInfo respond requestCollect : " + this._tvID);
            }
         }
         catch(e:Error)
         {
            _log.debug("CollectStateInfo requestCollect analyze fail : " + _tvID);
         }
      }
      
      public function requestUNCollect(param1:String, param2:Boolean) : void {
         var _loc3_:String = null;
         var _loc4_:String = null;
         if(param1)
         {
            this.destroyLoader();
            this._tvID = param1;
            _loc3_ = param2?SystemConfig.LOGIN_REQUEST_UNCOLLECT:SystemConfig.UNLOGIN_REQUEST_UNCOLLECT;
            _loc4_ = _loc3_ + "?tvIds=" + param1 + "&tn=" + Math.floor(Math.random() * 100000);
            this._request = new URLRequest(_loc4_);
            this._loader = new URLLoader();
            this._loader.addEventListener(Event.COMPLETE,this.onUNCollectCompleteHandler);
            this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
            this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
            this._loader.load(this._request);
            this._log.info("CollectStateInfo requestUNCollect: " + _loc4_);
         }
      }
      
      private function onUNCollectCompleteHandler(param1:Event) : void {
         var jsonInfo:Object = null;
         var var_2:Event = param1;
         try
         {
            jsonInfo = com.adobe.serialization.json.JSON.decode(this._loader.data);
            if(!(jsonInfo.code == undefined) && jsonInfo.code == "A00000")
            {
               this._isCollect = false;
               dispatchEvent(new Event(EVENT_COLLECT_STATE_CHANGE));
               this._log.info("CollectStateInfo respond requestUNCollect: " + this._tvID);
            }
         }
         catch(e:Error)
         {
            _log.debug("CollectStateInfo requestUNCollect analyze fail : " + _tvID);
         }
      }
      
      public function requestCollectState(param1:String, param2:Boolean) : void {
         var _loc3_:String = null;
         var _loc4_:String = null;
         if(param1)
         {
            this.destroyLoader();
            this._tvID = param1;
            _loc3_ = param2?SystemConfig.LOGIN_REQUEST_COLLECT_STATE:SystemConfig.UNLOGIN_REQUEST_COLLECT_STATE;
            _loc4_ = _loc3_ + "?tvId=" + param1 + "&tn=" + Math.floor(Math.random() * 100000);
            this._request = new URLRequest(_loc4_);
            this._loader = new URLLoader();
            this._loader.addEventListener(Event.COMPLETE,this.onStateCompleteHandler);
            this._loader.addEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
            this._loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
            this._loader.load(this._request);
            this._log.info("CollectStateInfo request requestCollectState: " + _loc4_);
         }
      }
      
      private function onStateCompleteHandler(param1:Event) : void {
         var jsonInfo:Object = null;
         var event:Event = param1;
         try
         {
            jsonInfo = com.adobe.serialization.json.JSON.decode(this._loader.data);
            if(!(jsonInfo.code == undefined) && jsonInfo.code == "A00000")
            {
               this._isCollect = !(jsonInfo.status == undefined) && jsonInfo.status == 1?true:false;
               this._isHasCollect = this._isCollect;
               dispatchEvent(new Event(EVENT_COLLECT_STATE_CHANGE));
               this._log.info("CollectStateInfo respond requestCollectState: " + this._tvID);
            }
         }
         catch(e:Error)
         {
            _log.debug("CollectStateInfo requestCollectState analyze fail : " + _tvID);
         }
      }
      
      private function onErrorHander(param1:*) : void {
         this._log.debug("CollectStateInfo onErrorHander : " + this._tvID);
      }
      
      private function destroyLoader() : void {
         if(this._loader)
         {
            this._loader.removeEventListener(Event.COMPLETE,this.onUNCollectCompleteHandler);
            this._loader.removeEventListener(Event.COMPLETE,this.onCollectCompleteHandler);
            this._loader.removeEventListener(Event.COMPLETE,this.onStateCompleteHandler);
            this._loader.removeEventListener(IOErrorEvent.IO_ERROR,this.onErrorHander);
            this._loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onErrorHander);
            try
            {
               this._loader.close();
            }
            catch(e:Error)
            {
            }
            this._request = null;
            this._loader = null;
         }
      }
      
      public function get isHasCollect() : Boolean {
         return this._isHasCollect;
      }
      
      public function get isCollect() : Boolean {
         return this._isCollect;
      }
      
      public function set isCollect(param1:Boolean) : void {
         this._isHasCollect = param1;
         this._isCollect = param1;
      }
   }
}
